package com.blrs.service.enterprise.businessinfo.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.core.domain.entity.SysUser;
import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.ReflectionUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.*;
import com.blrs.domain.vo.BusinessMainClassVo;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.mapper.*;
import com.blrs.service.enterprise.businessinfo.IBusinessOpportunityInformationService;
import com.blrs.system.mapper.SysUploadFileMapper;
import com.blrs.system.mapper.SysUserMapper;
import com.blrs.system.service.ISysDeptService;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-22
 */
@Service
public class BusinessOpportunityInformationServiceImpl implements IBusinessOpportunityInformationService {
    @Autowired
    private BusinessOpportunityInformationMapper businessOpportunityInformationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private EnterprisePersonInfoMapper enterprisePersonInfoMapper;
    @Autowired
    private HighSeasMapper highSeasMapper;
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    private ContractManagementInfoMapper contractManagementInfoMapper;

    @Autowired
    private QuotationInfoMapper quotationInfoMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;
    @Autowired
    private BusinessFollowAnnotaterMapper businessFollowAnnotaterMapper;
    @Autowired
    private BusinessFollowRecordsMapper businessFollowRecordsMapper;
    /**
     * 查询【请填写功能名称】
     *
     * @param businessId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public BusinessOpportunityInformation selectBusinessOpportunityInformationByBusinessId(String businessId) {
        List<BusinessOpportunityInformation> informationList = businessOpportunityInformationMapper.selectBusinessOpportunityInformationById(businessId);
        BusinessOpportunityInformation businessOpportunityInformation = new BusinessOpportunityInformation();
        if (informationList.size() > 0) {
            businessOpportunityInformation = businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(businessId);
        }
        return businessOpportunityInformation;
    }

    @Override
    public List<BusinessOpportunityInformation> selectBusinessOpportunityInformationById(String businessId) {
        return businessOpportunityInformationMapper.selectBusinessOpportunityInformationById(businessId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param businessOpportunityInformation 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<BusinessOpportunityInformation> selectBusinessOpportunityInformationList(BusinessOpportunityInformation businessOpportunityInformation) {
        if(SecurityUtils.getUserId()!=1&&SecurityUtils.getUserId()!=2){
           if (SecurityUtils.getLoginUser().getUser().getIsAdmin().equals("1")){
               businessOpportunityInformation.setBelongTo(SecurityUtils.getOrgId());
           }else {
               //说明是机构或者企业中的普通用户
               //2025-06-02 不仅是业务经理可以查看，商机的审核人和批注人也可以查看
               businessOpportunityInformation.setTransactionId(SecurityUtils.getUserId());
               businessOpportunityInformation.setBusinessStatusUser(SecurityUtils.getUserId());
               //查询当前用户所有可以批注的商机信息id
               List<String> businessIds =businessFollowAnnotaterMapper.selectBusinessIdByAnnotaterId(SecurityUtils.getUserId());
               businessOpportunityInformation.setBusinessIds(businessIds);
           }
        }
        List<BusinessOpportunityInformation> businessOpportunityInformations = businessOpportunityInformationMapper.selectBusinessOpportunityInformationList(businessOpportunityInformation);
//        for (BusinessOpportunityInformation opportunityInformation : businessOpportunityInformations) {
//            opportunityInformation.setBusinessManagerNickName(sysUserMapper.selectUserById(opportunityInformation.getTransactionId()).getNickName());
//            opportunityInformation.setCreationName(sysUserMapper.selectUserById(opportunityInformation.getCreationId()).getNickName());
//          if (opportunityInformation.getDevelopmentId() != null){
//              opportunityInformation.setDevelopmentManagerNickName(sysUserMapper.selectUserById(opportunityInformation.getDevelopmentId()).getNickName());
//          }
////          if (opportunityInformation.getCollaboratorId() != null){
////              opportunityInformation.setCollaboratorName(sysUserMapper.selectUserById(opportunityInformation.getCollaboratorId()).getNickName());
////          }
//        }
        return businessOpportunityInformations;
    }

    @Override
    public List<BusinessOpportunityInformation> selectBusinessOpportunityInformationReviewList(BusinessOpportunityInformation businessOpportunityInformation) {
        businessOpportunityInformation.setApprovalId(SecurityUtils.getUserId().toString());
        return businessOpportunityInformationMapper.selectBusinessOpportunityInformationReviewList(businessOpportunityInformation);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param businessOpportunityInformation 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBusinessOpportunityInformation(BusinessOpportunityInformation businessOpportunityInformation) throws NoSuchFieldException, IllegalAccessException {
        EnterprisePersonInfo info = new EnterprisePersonInfo();
        info.setPhone(businessOpportunityInformation.getTelephone());
        info.setEnterpriseName(businessOpportunityInformation.getEnterpriseName());
        info.setOrganizationId(SecurityUtils.getOrgId());
        List<EnterprisePersonInfo> personInfoList = enterprisePersonInfoMapper.selectEnterprisePersonInfoListByPhone(info);
        if (personInfoList.size() == 0) {
            EnterprisePersonInfo personInfo = new EnterprisePersonInfo();
            personInfo.setId(UUID.randomUUID().toString());
            personInfo.setPhone(businessOpportunityInformation.getTelephone());
            personInfo.setName(businessOpportunityInformation.getContacts());
            personInfo.setEnterpriseName(businessOpportunityInformation.getEnterpriseName());
            personInfo.setEnterpriseId(businessOpportunityInformation.getEnterpriseId());
            personInfo.setCreateId(SecurityUtils.getUserId());
            personInfo.setOrganizationId(SecurityUtils.getOrgId());
            personInfo.setOrganizationName(SecurityUtils.getOrgName());
            personInfo.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
            personInfo.setFilingTime(new Timestamp(new Date().getTime()));
            personInfo.setCreationTime(new Timestamp(new Date().getTime()));
            personInfo.setOperateTime(new Timestamp(new Date().getTime()));
            personInfo.setDelFlag(0);
            enterprisePersonInfoMapper.insertEnterprisePersonInfo(personInfo);
        } else {
            EnterprisePersonInfo enterprisePersonInfo = enterprisePersonInfoMapper.selectEnterprisePersonInfoById(personInfoList.get(0).getId());
            enterprisePersonInfo.setName(businessOpportunityInformation.getContacts());
            enterprisePersonInfo.setEnterpriseName(businessOpportunityInformation.getEnterpriseName());
            enterprisePersonInfo.setEnterpriseId(businessOpportunityInformation.getEnterpriseId());
            enterprisePersonInfo.setOrganizationId(SecurityUtils.getOrgId());
            enterprisePersonInfo.setOrganizationName(SecurityUtils.getOrgName());
            enterprisePersonInfo.setOperateTime(new Timestamp(new Date().getTime()));
            enterprisePersonInfoMapper.updateEnterprisePersonInfo(enterprisePersonInfo);
        }
        return businessOpportunityInformationMapper.insertBusinessOpportunityInformation(businessOpportunityInformation);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param businessOpportunityInformation 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBusinessOpportunityInformation(BusinessOpportunityInformation businessOpportunityInformation) throws NoSuchFieldException, IllegalAccessException {
        //审核之后不准删除
        businessFollowRecordsMapper.updateBusinessFollowExamine(businessOpportunityInformation.getRecordsId(), 1);
        BusinessOpportunityInformation business = businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(businessOpportunityInformation.getBusinessId());
        EnterprisePersonInfo info = new EnterprisePersonInfo();
        info.setPhone(businessOpportunityInformation.getTelephone());
        info.setEnterpriseName(businessOpportunityInformation.getEnterpriseName());
        info.setOrganizationId(SecurityUtils.getOrgId());
        List<EnterprisePersonInfo> personInfoList = enterprisePersonInfoMapper.selectEnterprisePersonInfoListByPhone(info);
        if (personInfoList.size() == 0) {
            EnterprisePersonInfo personInfo = new EnterprisePersonInfo();
            personInfo.setId(UUID.randomUUID().toString());
            personInfo.setPhone(businessOpportunityInformation.getTelephone());
            personInfo.setName(businessOpportunityInformation.getContacts());
            personInfo.setEnterpriseName(businessOpportunityInformation.getEnterpriseName());
            personInfo.setEnterpriseId(businessOpportunityInformation.getEnterpriseId());
            personInfo.setCreateId(SecurityUtils.getUserId());
            personInfo.setOrganizationId(SecurityUtils.getOrgId());
            personInfo.setOrganizationName(SecurityUtils.getOrgName());
            personInfo.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
            personInfo.setFilingTime(new Timestamp(new Date().getTime()));
            personInfo.setCreationTime(new Timestamp(new Date().getTime()));
            personInfo.setOperateTime(new Timestamp(new Date().getTime()));
            personInfo.setDelFlag(0);
            enterprisePersonInfoMapper.insertEnterprisePersonInfo(personInfo);
        } else {
            EnterprisePersonInfo enterprisePersonInfo = enterprisePersonInfoMapper.selectEnterprisePersonInfoById(personInfoList.get(0).getId());
            enterprisePersonInfo.setName(businessOpportunityInformation.getContacts());
            enterprisePersonInfo.setEnterpriseName(businessOpportunityInformation.getEnterpriseName());
            enterprisePersonInfo.setEnterpriseId(businessOpportunityInformation.getEnterpriseId());
            enterprisePersonInfo.setOrganizationId(SecurityUtils.getOrgId());
            enterprisePersonInfo.setOrganizationName(SecurityUtils.getOrgName());
            enterprisePersonInfo.setOperateTime(new Timestamp(new Date().getTime()));
            enterprisePersonInfoMapper.updateEnterprisePersonInfo(enterprisePersonInfo);
        }
        if (business == null) {
            //如果企业不存在，则新增企业信息到公海
            //highSeasMapper.selectHigSeasByOrgName(businessOpportunityInformation.getEnterpriseName(), SecurityUtils.getOrgId())==0
            if ( highSeasMapper.selectHigSeasAndEleAndEnter(businessOpportunityInformation.getEnterpriseName(), SecurityUtils.getOrgId())==0){
                //说明企业不存在，进行新增
                HighSeas highSeas = new HighSeas();
                ReflectionUtils.setCommonPrivateProperty(highSeas);
                businessOpportunityInformation.setEnterpriseId(highSeas.getEnterpriseId());
                highSeas.setEnterpriseName(businessOpportunityInformation.getEnterpriseName());
                highSeas.setContacts(businessOpportunityInformation.getContacts());
                highSeas.setTelephone(businessOpportunityInformation.getTelephone());
                highSeasMapper.insertHighSeas(highSeas);
            }
            businessOpportunityInformation.setDelFlag("0");
            businessOpportunityInformation.setCreationId(SecurityUtils.getUserId());
            businessOpportunityInformation.setOperateTime(new Timestamp(new Date().getTime()));
            businessOpportunityInformation.setOperateName(SecurityUtils.getUsername());
            businessOpportunityInformation.setOperateId(SecurityUtils.getUserId());
            businessOpportunityInformation.setOrganizationId(SecurityUtils.getOrgId());
            businessOpportunityInformation.setOrganizationName(SecurityUtils.getOrgName());
            businessOpportunityInformation.setDepartmentId(SecurityUtils.getDeptId());
            businessOpportunityInformation.setDepartmentName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
            businessOpportunityInformation.setBelongTo(SecurityUtils.getOrgId());
            return businessOpportunityInformationMapper.insertBusinessOpportunityInformation(businessOpportunityInformation);
        } else {
            businessOpportunityInformation.setOperateTime(new Timestamp(new Date().getTime()));
            businessOpportunityInformation.setOperateName(SecurityUtils.getUsername());
            businessOpportunityInformation.setOperateId(SecurityUtils.getUserId());
            return businessOpportunityInformationMapper.updateBusinessOpportunityInformation(businessOpportunityInformation);
        }
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param businessIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBusinessOpportunityInformationByBusinessIds(String[] businessIds) {
        //批量删除报价单
        quotationInfoMapper.deleteQuotationInfoIds(businessIds);
        //批量删除合同
        contractManagementInfoMapper.deleteBusinessByBusinessIds(businessIds);
        return businessOpportunityInformationMapper.deleteBusinessOpportunityInformationByBusinessIds(businessIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param businessId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBusinessOpportunityInformationByBusinessId(String businessId) {
        return businessOpportunityInformationMapper.deleteBusinessOpportunityInformationByBusinessId(businessId);
    }

    @Override
    public int editMainClass(BusinessMainClassVo businessMainClassVo) {
        boolean flag = businessMainClassVo.getBusinessId().contains(",");
        int count = 0;
        if (flag) {
            String[] businessIds = businessMainClassVo.getBusinessId().split(",");
            for (int i = 0; i < businessIds.length; i++) {
                BusinessOpportunityInformation information = businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(businessIds[i]);
                information.setClassificationId(businessMainClassVo.getMainClassId());
                information.setOperateTime(new Timestamp(new Date().getTime()));
                information.setOperateName(SecurityUtils.getUsername());
                information.setOperateId(SecurityUtils.getUserId());
                count = businessOpportunityInformationMapper.updateBusinessOpportunityInformation(information);
            }
        } else {
            BusinessOpportunityInformation information = businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(businessMainClassVo.getBusinessId());
            information.setClassificationId(businessMainClassVo.getMainClassId());
            information.setOperateTime(new Timestamp(new Date().getTime()));
            information.setOperateName(SecurityUtils.getUsername());
            information.setOperateId(SecurityUtils.getUserId());
            count = businessOpportunityInformationMapper.updateBusinessOpportunityInformation(information);
        }
        return count;
    }

    @Override
    public int editBusinessManager(BusinessMainClassVo businessMainClassVo) {
        boolean flag = businessMainClassVo.getBusinessId().contains(",");
        int count = 0;
        if (flag) {
            String[] businessIds = businessMainClassVo.getBusinessId().split(",");
            for (int i = 0; i < businessIds.length; i++) {
                BusinessOpportunityInformation information = businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(businessIds[i]);
                information.setTransactionId(businessMainClassVo.getTransactionId());
                information.setBusinessManager(businessMainClassVo.getBusinessManager());
                information.setOperateTime(new Timestamp(new Date().getTime()));
                information.setOperateName(SecurityUtils.getUsername());
                information.setOperateId(SecurityUtils.getUserId());
                count = businessOpportunityInformationMapper.updateBusinessOpportunityInformation(information);
            }
        } else {
            BusinessOpportunityInformation information = businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(businessMainClassVo.getBusinessId());
            information.setTransactionId(businessMainClassVo.getTransactionId());
            information.setBusinessManager(businessMainClassVo.getBusinessManager());
            information.setOperateTime(new Timestamp(new Date().getTime()));
            information.setOperateName(SecurityUtils.getUsername());
            information.setOperateId(SecurityUtils.getUserId());
            count = businessOpportunityInformationMapper.updateBusinessOpportunityInformation(information);
        }
        return count;
    }

    @Override
    public String importExcel(List<BusinessOpportunityInformation> list) throws NoSuchFieldException, IllegalAccessException {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入商机数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int personAddNum = 0;
        int personUpdateNum= 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BusinessOpportunityInformation information : list) {
            try {
                if (StringUtils.isEmpty(information.getBusinessName())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、商机名称为空！ ");
                } else if (StringUtils.isEmpty(information.getEnterpriseName())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、客户名称为空！ ");
                }else if (StringUtils.isEmpty(information.getBusinessType())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、商机类型为空！ ");
                }else if (StringUtils.isEmpty(information.getBusinessSource())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、商机来源为空！ ");
                }else if (StringUtils.isEmpty(information.getBusinessState())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、商机状态为空！ ");
                }else if (StringUtils.isEmpty(information.getContacts())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、联系人姓名为空！ ");
                }else if (StringUtils.isEmpty(information.getTelephone())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、联系人电话为空！ ");
                }else if (StringUtils.isEmpty(information.getBusinessProvider())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、商机提供人为空！ ");
                }else if (StringUtils.isEmpty(information.getExpectedDate().toString())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、预计成交时间为空！ ");
                }else if (StringUtils.isEmpty(information.getTrackingStage())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、跟单阶段为空！ ");
                }else if (StringUtils.isEmpty(information.getCollaboratorName())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、协作人为空！ ");
                }else if (StringUtils.isEmpty(information.getDevelopmentManager())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、开发经理为空！ ");
                }else if (StringUtils.isEmpty(information.getBusinessManager())) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、业务经理为空！ ");
                }else if(StringUtils.isNotEmpty(information.getFollowPerson())&&getUserIdByName(information.getFollowPerson())==null){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、跟单人不是该公司用户！ ");
                }else if(StringUtils.isNotEmpty(information.getDevelopmentManager())&&getUserIdByName(information.getDevelopmentManager())==null){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、开发经理不是该公司用户！ ");
                }else if(StringUtils.isNotEmpty(information.getBusinessManager())&&getUserIdByName(information.getBusinessManager())==null){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、业务经理不是该公司用户！ ");
                }else if(StringUtils.isNotEmpty(information.getDevelopmentManager())&&getUserIdByName(information.getDevelopmentManager())==null){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、协作人不是该公司用户！ ");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            for (BusinessOpportunityInformation information : list) {
                EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
                HighSeas highSeas = new HighSeas();
                highSeas.setEnterpriseName(information.getEnterpriseName());
                highSeas.setBelongTo(SecurityUtils.getOrgId());
                List<HighSeas> seas = enterpriseInfoMapper.selectHighSeasByEnterpriseName(highSeas);
                enterpriseInfo.setEnterpriseName(information.getEnterpriseName());
                enterpriseInfo.setBelongTo(SecurityUtils.getOrgId().toString());
                List<EnterpriseInfo> infos = enterpriseInfoMapper.selectEnterpriseInfoByEnterpriseNameAndBelongTo(enterpriseInfo);
                if(infos.size()>0){
                    information.setEnterpriseId(infos.get(0).getEnterpriseId());
                }else{
                    information.setEnterpriseId(seas.get(0).getEnterpriseId());
                }
                information.setBusinessId(UUID.randomUUID().toString());
                information.setCollaboratorId(getUserIdByName(information.getCollaboratorName()));
                information.setDevelopmentId(getUserIdByName(information.getDevelopmentManager()));
//                information.setDevelopmentManager(getNickNameByName(information.getDevelopmentManager()));
//                information.setBusinessManager(getNickNameByName(information.getBusinessManager()));
//                information.setCollaboratorName(getNickNameByName(information.getCollaboratorName()));
                information.setTransactionId(getUserIdByName(information.getBusinessManager()));
                information.setCreationId(SecurityUtils.getUserId());
                information.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
                information.setCreationName(SecurityUtils.getUsername());
                information.setCreationTime(new Timestamp(new Date().getTime()));
                information.setOperateTime(new Timestamp(new Date().getTime()));
                information.setOperateName(SecurityUtils.getUsername());
                information.setOperateId(SecurityUtils.getUserId());
                information.setOrganizationId(SecurityUtils.getOrgId());
                information.setOrganizationName(SecurityUtils.getOrgName());
                information.setDepartmentId(SecurityUtils.getDeptId());
                information.setDepartmentName(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
                information.setBelongTo(SecurityUtils.getOrgId());
                ReflectionUtils.setCommonPrivateProperty(information);
                businessOpportunityInformationMapper.insertBusinessOpportunityInformation(information);
                successNum++;
                successMsg.append("<br/>" + successNum + "、商机信息数据导入成功");

                EnterprisePersonInfo info = new EnterprisePersonInfo();
                info.setPhone(information.getTelephone());
                info.setEnterpriseName(information.getEnterpriseName());
                info.setOrganizationId(SecurityUtils.getOrgId());
                List<EnterprisePersonInfo> personInfoList = enterprisePersonInfoMapper.selectEnterprisePersonInfoListByPhone(info);
                if (personInfoList.size() == 0) {
                    EnterprisePersonInfo personInfo = new EnterprisePersonInfo();
                    personInfo.setId(UUID.randomUUID().toString());
                    personInfo.setPhone(information.getTelephone());
                    personInfo.setName(information.getContacts());
                    personInfo.setEnterpriseName(information.getEnterpriseName());
                    personInfo.setEnterpriseId(information.getEnterpriseId());
                    personInfo.setCreateId(SecurityUtils.getUserId());
                    personInfo.setOrganizationId(SecurityUtils.getOrgId());
                    personInfo.setOrganizationName(SecurityUtils.getOrgName());
                    personInfo.setClassificationId("650c44e1-b575-4b62-9d54-1ba9d1e8abb7");
                    personInfo.setFilingTime(new Timestamp(new Date().getTime()));
                    personInfo.setCreationTime(new Timestamp(new Date().getTime()));
                    personInfo.setOperateTime(new Timestamp(new Date().getTime()));
                    personInfo.setDelFlag(0);
                    enterprisePersonInfoMapper.insertEnterprisePersonInfo(personInfo);
                    personAddNum++;
                    successMsg.append("，人员信息新增成功");
                } else {
                    EnterprisePersonInfo enterprisePersonInfo = enterprisePersonInfoMapper.selectEnterprisePersonInfoById(personInfoList.get(0).getId());
                    enterprisePersonInfo.setName(information.getContacts());
                    enterprisePersonInfo.setEnterpriseName(information.getEnterpriseName());
                    enterprisePersonInfo.setEnterpriseId(information.getEnterpriseId());
                    enterprisePersonInfo.setOrganizationId(SecurityUtils.getOrgId());
                    enterprisePersonInfo.setOrganizationName(SecurityUtils.getOrgName());
                    enterprisePersonInfo.setOperateTime(new Timestamp(new Date().getTime()));
                    enterprisePersonInfoMapper.updateEnterprisePersonInfo(enterprisePersonInfo);
                    personUpdateNum++;
                    successMsg.append("，人员信息更新成功");
                }
            }
            successMsg.insert(0, "恭喜您，导入商机数据 " + successNum + " 条，新增人员数据"+personAddNum+"条，更新人员数据"+personUpdateNum+"条，数据如下：");
        }
        return successMsg.toString();
    }

    private Long getUserIdByName(String name) {
        SysUser user = sysUserMapper.selectUserByUserName(name);
        if (user != null) {
            return user.getUserId();
        } else {
            return null;
        }
    }

    private String getNickNameByName(String name) {
        SysUser user = sysUserMapper.selectUserByUserName(name);
        if (user != null) {
            return user.getNickName();
        } else {
            return null;
        }
    }

    /**
     * 分组商机提供人
     * @return
     */
    public List<SysUser>  groupBusinessProvider(){
        ArrayList<SysUser> sysUsers = new ArrayList<>();
        List<BusinessOpportunityInformation> businessOpportunityInformations = businessOpportunityInformationMapper.groupBusinessProvider(SecurityUtils.getOrgId());
        businessOpportunityInformations.stream().distinct().forEach(businessOpportunityInformation -> {
            SysUser sysUser = new SysUser();
            sysUser.setUserId(businessOpportunityInformation.getDevelopmentId());
            sysUser.setNickName(sysUserMapper.selectUserById(businessOpportunityInformation.getDevelopmentId()).getNickName());
            sysUsers.add(sysUser);
        });
        return sysUsers;
    }

    @Override
    public List<QuotationInfo> selectQuotationInfo(QuotationInfo quotationInfo) {
       if (SecurityUtils.getLoginUser().getUser().getIsAdmin().equals("1")){
           //管理员查看所有报价单
            quotationInfo.setBelongTo(SecurityUtils.getOrgId());
       }else{
           //非管理员查看自己负责的报价单
           quotationInfo.setApprovalId(String.valueOf(SecurityUtils.getUserId()));
           quotationInfo.setOperateId(String.valueOf(SecurityUtils.getUserId()));
       }
        List<QuotationInfo> quotationInfos = quotationInfoMapper.selectQuotationInfo(quotationInfo);
        for (QuotationInfo info : quotationInfos) {
            info.setCompanyName(businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(info.getBusinessId()).getEnterpriseName());
            info.setBusinessName(businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(info.getBusinessId()).getBusinessName());
            int count=sysUploadFileMapper.selectUrlByCodeCount("businessinfo", info.getUploadFile());
            info.setUrl(count > 0 ? String.valueOf(count) : null);
        }
        return quotationInfos;
    }
}
